package com.comdev.common;


import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.comdev.ut.xml.XmlUT;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.comdev.online.ConstOnline;
import com.comdev.ut.string.StringUT;


/**
 * Author: gnoloahs
 * Date: 2013-04-16
 * Time: 下午4:51
 */
public class AppInerceptor implements HandlerInterceptor
{

    // 进入control前
    @Override
    public boolean preHandle(HttpServletRequest httpreq, HttpServletResponse reps, Object o)
            throws Exception
    {


//        if (!StringUT.isChrome(httpreq) && !StringUT.isFirefox(httpreq) && !StringUT.isSWF(httpreq))
//        {
//            reps.setCharacterEncoding("UTF-8");
//            reps.addHeader("Content-Type", "text/html; charset=UTF-8");
//            reps.getWriter().write("请使用Firefox或Chrome访问本系统");
//
//            return false;
//        }

        // 如果是swf的上传请求--把里面的JSESIONID重新写入request中
        if (StringUT.isSWF(httpreq))
        {
            // StringUT.setJSESSIONID(httpreq,httpreq.getParameter("JSESSIONID"),reps);
            String jsessionid = httpreq.getParameter("JSESSIONID");//当这里总是取不到值的时候,看看multipartResolver是否在mvc-servlet,xml中配置了
            HttpSession session = httpreq.getSession();

            session.setAttribute("curuser", ConstOnline.onlineUsers.get(jsessionid));
            Object obj = session.getAttribute("curuser");
            return true;
        }

        HttpSession session = httpreq.getSession();

        //如果是调试，那么不判断用户是否登录
        if(XmlUT.getValue("//debug", WebPath.getClassRootPath()+"/sys.xml").equals("1"))
        {
            return true;
        }
        if (!StringUT.isEmpty(session.getAttribute("curuser")) || inPassUrl(httpreq.getPathInfo()))
        {
            return true;
        }
        reps.setCharacterEncoding("UTF-8");
        reps.addHeader("Content-Type", "text/html; charset=UTF-8");
        reps.getWriter().write("{\"reps_code\":\"-1\",\"reps_des\":\"登录超时或尚未登录\"}");
        return false;
    }


    private boolean inPassUrl(String cur)
    {
        if (cur.equals("/"))
        {
            return true;
        }
        List<String> passurls = new ArrayList<String>();
        passurls.add("/login/login");
        passurls.add(".js");
        passurls.add(".jpg");
        passurls.add(".png");
        passurls.add(".gif");
        passurls.add(".css");
        for (int i = 0; i < passurls.size(); i++)
        {
            String pass = passurls.get(i);
            if (cur.contains(pass))
            {
                return true;
            }
        }
        return false;
    }


    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView)
            throws Exception
    {

    }


    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e)
            throws Exception
    {

    }
}
